<?php

namespace app\api\controller;

use think\Db;

//收货地址
//type 1 寄货地址 0 收货地址
class Address extends BaseController
{
    //获取微信共享收货地址
    public function getAddressApi()
    {
        $APPID = C('APPID');
        $SCRETID = C('SCRETID');
        if (!isset($_GET['code'])) {
            $backurl = $this->get_url();
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($backurl).'&response_type=code&scope=jsapi_address&state=123#wechat_redirect';
            // snsapi_userinfo
            header("Location: $url");
            exit;
        } else {
            $code = $_GET['code'];
            $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$APPID.'&secret='.$SCRETID.'&code='.$code.'&grant_type=authorization_code';
            $re = file_get_contents($url);
            $rearr = json_decode($re, true);
            $backurl = $this->get_url();
            $openid = $rearr['openid'];
            $unionid = $rearr['unionid'];
            $asstoken = $rearr['access_token'];
            S('jsapi_address_token'.$openid, $asstoken, 7200);
            $data['appid'] = $APPID;
            $data['url'] = $backurl;
            $data['timestamp'] = strval(time());
            $data['noncestr'] = rand(100000, 999999);
            $data['accesstoken'] = $asstoken;
            foreach ($data as $k => $v) {
                $Parameters[$k] = $v;
            }
            //签名步骤一：按字典序排序参数
            ksort($Parameters);
            $String = $this->formatBizQueryParaMap($Parameters, false);
            $data['addrsign'] = sha1($String);
            $this->assign('data', $data);
        }
        $this->siteDisplay('address_api');
    }

    //获取所有收货地址
    public function getAll()
    {
        $user_info = session('user_info');
        $data = Db::name('address')->where(['user_id' => $user_info['id'], 'type' => 0])->select();

        return success($data);
    }

    //获取所有快递
    public function getAllExpress()
    {
        $data = Db::name('express')->select();

        return success($data);
    }

    //修改、添加收货地址
    public function editAddress()
    {
        $user_info = session('user_info');
        $data = input('post.', '');
        if (!$data['consignee']) {
            return error('请填写收货人姓名');
        }
        if (!checkPhone($data['phone'])) {
            return error('请正确填写收货人的手机号');
        }
        if (!$data['province']) {
            return error('请填写收货人省份');
        }
        if (!$data['city']) {
            return error('请填写收货人城市');
        }
        if (!$data['county']) {
            return error('请填写收货人地区');
        }
        if (!$data['street']) {
            return error('请填写收货人街道详细地址');
        }
        $row = [
            'user_id' => $user_info['id'],
            'consignee' => strval($data['consignee']),
            'phone' => strval($data['phone']),
            'address' => strval($data['address']),
            'province' => strval($data['province']),
            'city' => strval($data['city']),
            'county' => strval($data['county']),
            'street' => strval($data['street']),
            'type' => 0,
            'position' => ['lng' => floatval($data['lng']), 'lat' => floatval($data['lat'])],
        ];
        if(!$row['address']){
            $row['address'] = ($row['province']==$$row['city']?'':$row['province']).$row['city'].$row['county'].$row['street'];
        }
        if (24 == strlen($data['id'])) {
            $findData = Db::name('address')->where(['user_id' => $user_info['id'], 'id' => $data['id'], 'type' => 0])->find();
            if (!$findData) {
                return error('未找到该收货地址');
            }
            Db::name('address')->where(['user_id' => $user_info['id'], 'id' => $data['id'], 'type' => 0])->update($row);
        } else {
            Db::name('address')->insert($row);
        }

        return success();
    }

    //获取寄货信息
    public function getSendAddress()
    {
        $user_info = session('user_info');
        $data = Db::name('address')->where(['user_id' => $user_info['id'], 'type' => 1])->find();

        return success($data);
    }

    //修改寄货信息
    public function editSendAddress()
    {
        $user_info = session('user_info');
        $data = input('post.', '');
        if (!$data['consignor']) {
            return error('请填写寄货人姓名');
        }
        if (!checkPhone($data['phone'])) {
            return error('请正确填写寄货人的手机号');
        }
        Db::name('address')->where(['user_id' => $user_info['id'], 'type' => 1])->update(['phone' => $data['phone'], 'consignor' => $data['consignor']], ['upsert' => true]);

        return success($data);
    }
}
